package net.zzy.pbbot.plugin.callMeStudy.api;

import net.lz1998.pbbot.bot.Bot;
import net.lz1998.pbbot.bot.BotPlugin;
import net.lz1998.pbbot.utils.Msg;
import net.zzy.pbbot.plugin.callMeStudy.service.CallMeService;
import onebot.OnebotEvent;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static net.zzy.pbbot.finalfield.Command.ADD_CALL;

/**
 * @author zhuZhaoYang
 * @date 2021/9/29 20:32
 */
@Component
public class AddCall extends BotPlugin {
    private CallMeService callMeService;

    @Autowired
    public void setCallMeService(CallMeService callMeService) {
        this.callMeService = callMeService;
    }

    @Override
    public int onGroupMessage(@NotNull Bot bot, @NotNull OnebotEvent.GroupMessageEvent event) {
        String rawMessage = event.getRawMessage();
        if (!rawMessage.startsWith(ADD_CALL)) {
            return MESSAGE_IGNORE;
        }
        Msg msg = callMeService.addCall(String.valueOf(event.getUserId()));
        msg.sendToGroup(bot, event.getGroupId());
        return MESSAGE_BLOCK;
    }

    @Override
    public int onPrivateMessage(@NotNull Bot bot, @NotNull OnebotEvent.PrivateMessageEvent event) {
        String rawMessage = event.getRawMessage();
        if (!rawMessage.startsWith(ADD_CALL)) {
            return MESSAGE_IGNORE;
        }
        Msg msg = callMeService.addCall(String.valueOf(event.getUserId()));
        msg.sendToFriend(bot, event.getUserId());
        return MESSAGE_BLOCK;
    }
}
